define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/qywx/contacts/index' + location.search,
                    add_url: 'facrm/qywx/contacts/add',
                    edit_url: 'facrm/qywx/contacts/edit',
                    del_url: 'facrm/qywx/contacts/del',
                    multi_url: 'facrm/qywx/contacts/multi',
                    import_url: 'facrm/qywx/contacts/import',
                    table: 'facrm_qywx_contacts',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'corp_id', title: __('Corp_id'), operate: 'LIKE'},
                        {field: 'external_userid', title: __('External_userid'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'unionid', title: __('Unionid'), operate: 'LIKE'},
                        {field: 'corp_full_name', title: __('Corp_full_name'), operate: 'LIKE'},
                        {field: 'corp_name', title: __('Corp_name'), operate: 'LIKE'},
                        {field: 'type', title: __('Type')},
                        {field: 'gender', title: __('Gender')},
                        {field: 'status', title: __('Status'), searchList: {"30":__('Status 30')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'customer_id', title: __('Customer_id')},
                        {field: 'customer_contact_id', title: __('Customer_contact_id')},
                        {field: 'customer.id', title: __('Customer.id')},
                        {field: 'customer.name', title: __('Customer.name'), operate: 'LIKE'},
                        {field: 'customer.mobile', title: __('Customer.mobile'), operate: 'LIKE'},
                        {field: 'customer.telephone', title: __('Customer.telephone'), operate: 'LIKE'},
                        {field: 'customer.deal_status', title: __('Customer.deal_status')},
                        {field: 'customer.deal_time', title: __('Customer.deal_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'customer.is_lock', title: __('Customer.is_lock')},
                        {field: 'customer.level', title: __('Customer.level')},
                        {field: 'customer.industry', title: __('Customer.industry')},
                        {field: 'customer.tags', title: __('Customer.tags'), operate: 'LIKE', formatter: Table.api.formatter.flag},
                        {field: 'customer.source', title: __('Customer.source')},
                        {field: 'customer.spread_uid', title: __('Customer.spread_uid')},
                        {field: 'customer.create_user_id', title: __('Customer.create_user_id')},
                        {field: 'customer.owner_user_id', title: __('Customer.owner_user_id')},
                        {field: 'customer.owner_user_ids', title: __('Customer.owner_user_ids'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'customer.user_id', title: __('Customer.user_id')},
                        {field: 'customer.ro_user_id', title: __('Customer.ro_user_id'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'customer.rw_user_id', title: __('Customer.rw_user_id'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'customer.province', title: __('Customer.province')},
                        {field: 'customer.city', title: __('Customer.city')},
                        {field: 'customer.area', title: __('Customer.area')},
                        {field: 'customer.detail_address', title: __('Customer.detail_address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'customer.lng', title: __('Customer.lng'), operate:'BETWEEN'},
                        {field: 'customer.lat', title: __('Customer.lat'), operate:'BETWEEN'},
                        {field: 'customer.next_time', title: __('Customer.next_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'customer.follow_time', title: __('Customer.follow_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'customer.create_time', title: __('Customer.create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'customer.update_time', title: __('Customer.update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'customer.delete_time', title: __('Customer.delete_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'customer.collect_time', title: __('Customer.collect_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'customer.weixin', title: __('Customer.weixin'), operate: 'LIKE'},
                        {field: 'customer.qq', title: __('Customer.qq')},
                        {field: 'customer.purchase_total', title: __('Customer.purchase_total'), operate:'BETWEEN'},
                        {field: 'customer.purchase_times', title: __('Customer.purchase_times')},
                        {field: 'customer.follow_status', title: __('Customer.follow_status'), formatter: Table.api.formatter.status},
                        {field: 'customer.service', title: __('Customer.service'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'customer.keynote', title: __('Customer.keynote'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'customer.survey1_count', title: __('Customer.survey1_count')},
                        {field: 'contacts.id', title: __('Contacts.id')},
                        {field: 'contacts.customer_id', title: __('Contacts.customer_id')},
                        {field: 'contacts.name', title: __('Contacts.name'), operate: 'LIKE'},
                        {field: 'contacts.mobile', title: __('Contacts.mobile'), operate: 'LIKE'},
                        {field: 'contacts.birthday', title: __('Contacts.birthday'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'contacts.telephone', title: __('Contacts.telephone'), operate: 'LIKE'},
                        {field: 'contacts.wechat', title: __('Contacts.wechat'), operate: 'LIKE'},
                        {field: 'contacts.email', title: __('Contacts.email'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'contacts.decision', title: __('Contacts.decision')},
                        {field: 'contacts.post', title: __('Contacts.post'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'contacts.sex', title: __('Contacts.sex')},
                        {field: 'contacts.detail_address', title: __('Contacts.detail_address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'contacts.ro_user_id', title: __('Contacts.ro_user_id'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'contacts.rw_user_id', title: __('Contacts.rw_user_id'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'contacts.create_user_id', title: __('Contacts.create_user_id')},
                        {field: 'contacts.owner_user_id', title: __('Contacts.owner_user_id')},
                        {field: 'contacts.next_time', title: __('Contacts.next_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'contacts.uid', title: __('Contacts.uid')},
                        {field: 'contacts.create_time', title: __('Contacts.create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'contacts.update_time', title: __('Contacts.update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'contacts.delete_time', title: __('Contacts.delete_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"), function (data, ret) {
		        setTimeout(function () {
		            location.href = ret.url;
		        }, 1500);
		    });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
